/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.IO;
using Microsoft.Win32;

namespace UnitedBinary.HarnessIt.UnitTests.Settings
{
	public class UnitTestSettings
	{
		private static string bad = null;
		
		private UnitTestSettings()
		{
		}

		public static string TestDataDirectory
		{
			get
			{
				try
				{
					string dir = "";
			
					string mainKey = @"Software\United Binary\HarnessIt\Development";
					string keyNameDataDir = "UnitTestDataDirectory";

					RegistryKey key = Registry.LocalMachine.OpenSubKey(mainKey);
					dir = (string)key.GetValue(keyNameDataDir);

					char [] chs = {'\\'};
					dir = dir.TrimEnd(chs);
					dir += "\\";

					return dir;
				}
				catch(Exception e)
				{
					string msg = "\n\nYou must set the value of the HarnessIt unit test data path on this system.\n";
					msg += "Do this by creating the registry key HLM\\SOFTWARE\\United Binary\\HarnessIt\\Development.\n";
					msg += "Then set the named value UnitTestDataDirectory to be the full path to that data directory.\n";
					msg += "That is, set its value to be the physical path to the directory .\\HarnessIt\\UnitTests\\Test Data Files\n";
					msg += "See the UnitTestSettings.cs file in the Unit Tests project for more details.\n";
					msg += "\n";

					throw new Exception(msg,e);
				}
			}
		}

		public static string BaselineAssemblyPath
		{
			get
			{
				if (bad == null)
				{
					try
					{
						string mainKey = @"Software\United Binary\HarnessIt\Development";
						string keyNameDataDir = "BaselineAssemblyPath";
					
						RegistryKey key = Registry.LocalMachine.OpenSubKey(mainKey);
						bad = (string)key.GetValue(keyNameDataDir);
					}
					catch(Exception e)
					{
						string msg = "\n\nYou must set the value of the BaselineAssembly path on this system.\n";
						msg += "Do this by creating the registry key HKLM\\SOFTWARE\\United Binary\\HarnessIt\\Development.\n";
						msg += "Next, set the named value BaselineAssemblyPath to the full path (including filename) of the assembly.\n";
						msg += "See the UnitTestSettings.cs file in the Unit Tests project for more details.\n";
						msg += "\n";

						throw new Exception(msg,e);
					}
				}
				return bad;
			}
		}

		public static string TestDataWriteDirectory
		{
			get
			{
				char [] chs = {'\\'};
				string dirBase = TestDataDirectory.TrimEnd(chs);
				
				string dir = dirBase + "\\TestOutput\\";
				System.IO.DirectoryInfo  di = new DirectoryInfo(dir);

				if (!di.Exists)
				{
					di.Create();
				}

				return dir;
			}
		}

	}
}
